package com.leetcode.No0581;

import org.junit.Test;

import java.util.Arrays;

/**
 * @program: leetcode
 * @description: 最短无序连续子数组
 * @author: wangzhihua
 * @date: 2021-08-14
 */
public class Solution01 {

	@Test
	public void test01() {
		int[] nums = new int[]{2,6,4,8,10,9,15};
		System.out.println(findUnsortedSubarray(nums));
	}

	public int findUnsortedSubarray(int[] nums) {
		int[] nums2 = Arrays.copyOf(nums, nums.length);
		Arrays.sort(nums2);
//		System.out.println(Arrays.toString(nums));
//		System.out.println(Arrays.toString(nums2));
		int left = 0, right = nums.length - 1;
		while (left <= nums.length - 1) {
			if (nums[left] != nums2[left]) {
				break;
			}
			++left;
		}
		while (right >= 0) {
			if (nums[right] != nums2[right]) {
				break;
			}
			--right;
		}
		if (left < right) {
			return right - left + 1;
		}
		return 0;
	}

}
